<template>
  <!-- 战败车型 -->
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="2"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      height="auto"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">加入黑名单</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  queryWhiteModels,
  getOrgList,
  updDealerBlacklist
 } from '@/api/potenCus/basedata/dealerBlacklist';
import { dictionary } from '@/utils/dictMixin';
import dealerBlacklistModel from './dealerBlacklistModel';
export default {
  name: 'dealerBlacklistModels',
  mixins: [dictionary],
  components: {
    dealerBlacklistModel
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: queryWhiteModels,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf:{ add: '新增', edit: '编辑', show: '显示' },
      selectedRow: null
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '关键字',
          fieldName: 'dealerCode',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '所属事业部',
          fieldName: 'orgId',
          placeholder: '请选择',
          itemList: this.createDictList('1045')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '所属事业部',
          dataIndex: 'orgName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `设置${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    selectChangeHandle(rows) {
      if (!rows.length) return;
      this.selectedRow = rows;
    },

    closeDialog(param) { //取消按钮
      this.$emit('close', false, param);
    },
    async saveDialog (param){ //加入黑名单
      // console.log('加入黑名单',this.selectedRow)
      const res = await updDealerBlacklist({ dealerList:this.selectedRow,status:10041002 });
      if (res.resultCode === 200) {
        this.$message.success("加入黑名单成功!");
        this.closeDialog('refresh')
      }
    },
    async getOrgList() {
      const res = await getOrgList();
      if (res.resultCode === 200) {
        this.topFilterList[1].itemList = res.data.map(x => ({ value: x.orgId, text: x.orgName }));
      }
    },
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getOrgList()
  }
};
</script>

<style lang="less" scoped>
</style>
